/**
 * 
 */
package idv.takeshi.software.productline.bookstore.presentation.web.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import idv.takeshi.software.productline.bookstore.domain.model.category.Category;
import idv.takeshi.software.productline.bookstore.domain.model.valueobject.InternationalText;


/**
 * Show Categories-tree to jsp page.
 * @author takeshi
 *
 */
public class CategoriesTreeTagHandler extends AbstractSimpleTagSupport {
	
	private static final Log log = LogFactory.getLog(CategoriesTreeTagHandler.class);
	
	private static final String INDENT_STRING = "\t";
	
	private Category root;
	
	private String id;
	
	private String hasBookName;
	
	private String rootCategoryTypeName = "rootCategoryType";
	
	private String categoryTypeName = "categoryType";
	
	private String bookTypeName = "bookType";
	
	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
	 */
	@Override
	public void doTag() throws JspException, IOException {
		super.doTag();
		JspWriter writer = this.getJspContext().getOut();
		String indentString = INDENT_STRING;
		writer.println("<div id=\"" + this.id + "\">");
		writer.println("<ul>");
		writer.print(indentString + "<li id=\"" + this.root.getId() + "\" rel=\"" + 
				this.rootCategoryTypeName + "\">");
		
		printAnchorsByIText(writer, indentString, this.root);
		
		if(this.root.getSubCategories().size() > 0){
			indentString += INDENT_STRING;
			writer.println(indentString + "<ul>");
			for(Category subCategory : this.root.getSubCategories()){
				printCategories(writer, indentString, subCategory);
			}
			writer.println(indentString + "</ul>");
		}
		writer.println(indentString + "</li>");
		writer.println("</ul>");
		writer.println("</div>");
		
	}


	/**
	 * @return the root
	 */
	public Category getRoot() {
		return root;
	}

	/**
	 * @param root the root to set
	 */
	public void setRoot(Category root) {
		this.root = root;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}


	/**
	 * @return the hasBookName
	 */
	public String getHasBookName() {
		return hasBookName;
	}


	/**
	 * @param hasBookName the hasBookName to set
	 */
	public void setHasBookName(String hasBookName) {
		this.hasBookName = hasBookName;
	}


	/**
	 * @return the rootCategoryTypeName
	 */
	public String getRootCategoryTypeName() {
		return rootCategoryTypeName;
	}


	/**
	 * @param rootCategoryTypeName the rootCategoryTypeName to set
	 */
	public void setRootCategoryTypeName(String rootCategoryTypeName) {
		this.rootCategoryTypeName = rootCategoryTypeName;
	}


	/**
	 * @return the categoryTypeName
	 */
	public String getCategoryTypeName() {
		return categoryTypeName;
	}


	/**
	 * @param categoryTypeName the categoryTypeName to set
	 */
	public void setCategoryTypeName(String categoryTypeName) {
		this.categoryTypeName = categoryTypeName;
	}


	/**
	 * @return the bookTypeName
	 */
	public String getBookTypeName() {
		return bookTypeName;
	}


	/**
	 * @param bookTypeName the bookTypeName to set
	 */
	public void setBookTypeName(String bookTypeName) {
		this.bookTypeName = bookTypeName;
	}


	/**
	 * Print anchors by InternationalText.neededLocales.
	 * @param writer
	 * @param indentString
	 * @throws IOException
	 */
	private static void printAnchorsByIText(JspWriter writer, String indentString, Category category)
			throws IOException {
		
		for(String locale : InternationalText.getNeededLocales()){
			writer.print(indentString + "<a href=\"#\" class=\"" + locale + "\">");
			writer.print(category.getName().getText(locale));
			writer.println("</a>");
		}
	}


	/**
	 * Print anchors by InternationalText.neededLocales and a given unique anchorText.
	 * @param writer
	 * @param indentString
	 * @throws IOException
	 */
	private static void printAnchorsByIText(JspWriter writer, String indentString, String anchorText)
			throws IOException {
		
		for(String locale : InternationalText.getNeededLocales()){
			writer.print(indentString + "<a href=\"#\" class=\"" + locale + "\">");
			writer.print(anchorText);
			writer.println("</a>");
		}
	}


	private void printCategories(JspWriter writer, String indentString, Category category) 
		throws IOException{
		try {
			writer.println(indentString + "<li id=\"" + category.getId() + 
					"\" rel=\"" + this.categoryTypeName +"\">");
			
			printAnchorsByIText(writer, indentString, category);
			
			if(category.hasSubCategories()){
				indentString += indentString;
				writer.println(indentString + "<ul>");
				for(Category subCategory : category.getSubCategories()){
					printCategories(writer, indentString, subCategory);
				}
				writer.println(indentString + "</ul>");
			} else if(category.hasBooks()){
				indentString += indentString;
				writer.println(indentString + "<ul>");
				writer.println(indentString + "<li rel=\"" + this.bookTypeName + "\">");
				//TODO TBD
				printAnchorsByIText(writer, indentString, this.hasBookName);
				writer.println(indentString + "</li>");
				writer.println(indentString + "</ul>");
			}
			writer.println(indentString + "</li>");
		} catch (IOException e) {
			String msg = "generate categories content error";
			log.error(msg, e);
			throw e;
		}
	}
	
	
	
	
	
}
